﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.List
{

    /// <summary>
    /// a = [1,2,3];
    /// b = [4,5];
    /// c = join(a, b);
    /// echo(c);
    /// </summary>
    public class JoinNode : FuncNode
    {

        public JoinNode()
            : base()
        {
            this.Value = "join";
        }

        public override object Evaluate()
        {
            ListObject result = new ListObject();

            for(int index = Parameters.Count-1;
                index>=0;
                --index)
            {
                ListObject lst 
                    = Parameters[index].Evaluate() as ListObject;
                if (lst == null)
                {
                    throw new Exception("Parameter is not a list");
                }

                result.AddRange(lst);
            }

            return result;
        }
    }
}
